core.OneLineHelper = function(aParams){
	this.init(aParams);
}

var textWidthHelper = core.newDiv(document.body, 'text_width_helper');
$(textWidthHelper).css("left", 0).css("top", "0");
core.extendClass('core.OneLineHelper', 'core.Box');

core.OneLineHelper.prototype.getTextWidth = function(aText, aClass){
	if(aClass)
		$(textWidthHelper).addClass(aClass);
	$(textWidthHelper).html(this.imagesToHTML(this.icon)+this.imagesToHTMLSprite(this.icon_cls)+'&nbsp;'+(aText || this.text));
	var result = $(textWidthHelper).width();
	if(aClass)
		$(textWidthHelper).removeClass(aClass);
	return result;
}

core.OneLineHelper.prototype.setText = function(aText){
	this.text = aText;
	this.isDirty = true;
//	$(this.textDiv).html(aText);//Images also
}


core.OneLineHelper.prototype.init = function(aParams){
	this.xType = 'oneline';
	this.superCall('init', arguments, aParams);
	this.isDirty = true;
	if(aParams.parent)
		aParams.parent.reParent(this);
	this.icon = aParams.icon;
	this.icon_cls = aParams.icon_cls;
	this.padding = aParams.padding || 5;
	this.textDiv = core.newDiv(this.div, 'oneline');
	this.align = aParams.align || 'left';
	this.setText(aParams.text);
    this.minHeight = 16;
	if(aParams.cls)
		$(this.div).addClass(aParams.cls);
	var instance = this;
	$(this.textDiv).click(function(e){
		return instance.fire('click', e);
	});
	$(this.textDiv).dblclick(function(e){
		return instance.fire('dblclick', e);
	});
	$(this.div).click(function(e){
		return instance.fire('click', e);
	});
	$(this.div).dblclick(function(e){
		return instance.fire('dblclick', e);
	});
	$(this.div).mousedown(function(e){
		return instance.fire('mousedown', e);
	});
	$(this.div).mouseup(function(e){
		return instance.fire('mouseup', e);
	});
	$(this.div).mouseover(function(e){
		return instance.fire('mouseover', e);
	});
	$(this.div).mouseout(function(e){
		return instance.fire('mouseout', e);
	});
	$(this.div).mousemove(function(e){
		return instance.fire('mousemove', e);
	});
};

core.OneLineHelper.prototype.reduceTextWidth = function(aDiv, aImages, aText, aWidth){
//	if(this.isDirty){
//		//add images to text
//		var suffix = '';
//		while(this.getTextWidth(aText+suffix)>aWidth){
//			if(!aText){
//				aText = '';
//				suffix = '';
//				break;
//			}
//			suffix = '...';
//			aText = aText.substring(0, aText.length-2);
//		}
//		this.reducedText = (aText || '')+suffix;
//		this.isDirty = false;
//	}
    this.reducedText = (aText || '');
	$(aDiv).html(this.imagesToHTML(this.icon)+this.imagesToHTMLSprite(this.icon_cls)+'&nbsp;'+this.reducedText);
	return aDiv;
}

core.OneLineHelper.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	this.reduceTextWidth(this.textDiv, [], this.text, aWidth-2*this.padding);
	this.alignBlock(this.textDiv, aWidth-2*this.padding, aHeight, this.align, 'middle', this.padding);
};

core.Tab = function(aParams){
	this.init(aParams);
}

core.extendClass('core.Tab', 'core.Box');

core.Tab.prototype.init = function(aParams){
	this.xType = 'tab';
	this.superCall('init', arguments, aParams);
    this.text = aParams.text;
    this.icon = aParams.icon;
	this.minWidth = TAB_WIDTH;
	this.menu = aParams.menu
	this.minHeight = BUTTON_HEIGHT;	
    this.cls = 'tab_bg';
    if(aParams.cls)
        this.cls = aParams.cls;
	this.tabDiv = core.newDiv(this.div, this.cls+' tab');
	$(this.tabDiv).height(this.minHeight);
    this.tabLeft = core.newDiv(this.div, this.cls+' tab_left');
    this.tabRight = core.newDiv(this.div, this.cls+' tab_right');
    this.closeable = aParams.closeable;
	var instance = this;

    if(this.closeable){
        this.closeDiv = core.newDiv(this.div, 'close_tab');
        $(this.closeDiv).addClass('icon tab_icon tab_close');
        $(this.closeDiv).click(function(e){
            instance.fire('close', e);
            return false;
        });
    }
	this.caption = new core.OneLineHelper({
		text: this.text,
		cls: 'mouse_over tab_text',
		icon_cls: this.icon,
		padding: 5,
        align: aParams.align,
		parent: this
	});
//	this.reParent(this.caption);
    this.div.oncontextmenu = function(){
        return false;
    };
//
    this.skipClick = false;

	this.caption.on('click', function(i, e){
        if(instance.skipClick){
            instance.skipClick = false;
            return false;
        }
        return instance.fire('select', e);
    });

	this.caption.on('mousedown', function(i, e){
        var rightClick = $.browser.opera?(e.shiftKey && e.which==1):(e.which==3);
		var showMenu = instance==instance.parent.selectedTab && !instance.parent.dropDown;
		if(showMenu && instance.menu && rightClick){
            instance.skipClick = true;
			var menu = [];
			jQuery.each(instance.menu, function(i, e){
				menu.push(e);
			});
			instance.parent.showDropDown(instance, menu, instance.parent.topDown,
                    false, instance.params.menuOwner);
//            return false;
        }
		return true;
	});	
};

core.Tab.prototype.select = function(){
	this.selected = true;
	$(this.tabDiv).addClass('tab_selected');
    $(this.tabLeft).addClass('tab_left_selected');
    $(this.tabRight).addClass('tab_right_selected');
}

core.Tab.prototype.unselect = function(){
	this.selected = false;
	$(this.tabDiv).removeClass('tab_selected');	
    $(this.tabLeft).removeClass('tab_left_selected');
    $(this.tabRight).removeClass('tab_right_selected');
}

core.Tab.prototype.setText = function(aText, aIcon){
    this.text = aText;
    this.icon = aIcon;
    this.caption.icon_cls = aIcon;
    this.refresh();
};

core.Tab.prototype.makeResize = function(aX, aY, aWidth, aHeight){
    var tabWidth = aWidth-2;
    if(this.closeable){
        tabWidth -= 20;
        this.alignBlock(this.closeDiv, aWidth-2, aHeight, 'right', 'middle');
    }
//	$(this.tabDiv).width(aWidth);
	this.caption.setText(core.escapeHTML(this.text));

	this.caption.resize(0, 0, tabWidth, aHeight);
}

core.Tabs = function(aParams){
	this.init(aParams);
};

core.extendClass('core.Tabs', 'core.Box');

core.VERTICAL_TOOLBAR_WIDTH = 40;

core.Tabs.prototype.init = function(aParams){
	this.xType = 'tabs';
	this.superCall('init', arguments, aParams);
	this.tabs = [];
	
	this.toolbarStyle = aParams.toolbar;
    if(this.toolbarStyle){
        this.vertical = aParams.vertical;
        if(!aParams.cls && !this.vertical)
            aParams.cls = 'toolbar';
    }
    if(aParams.cls){
        $(this.div).addClass(aParams.cls);
    }
	this.topDown = aParams.topDown;
	
	this.leftButton = new core.form.Button({
		icon: 'tab_icon tab_left_btn'
	});
	this.rightButton = new core.form.Button({
		icon: 'tab_icon tab_right_btn'
	});
	var instance = this;
	this.leftButton.on('press', function(){
		if(instance.startTab>0){
			instance.startTab--;
            instance.dirty = true;
			instance.resize(instance.bounds.x, instance.bounds.y, instance.bounds.width, instance.bounds.height);
		}
	});
	
	this.rightButton.on('press', function(){
		if(instance.startTab+instance.tabsShow<instance.tabs.length){
			instance.startTab++;
            instance.dirty = true;
			instance.resize(instance.bounds.x, instance.bounds.y, instance.bounds.width, instance.bounds.height);
		}
	});
	this.reParent(this.leftButton);
	this.reParent(this.rightButton);
	
	this.minWidth = aParams.minWidth || TAB_WIDTH+2*(BUTTON_WIDTH_ICON+TAB_PADDING);
	this.minHeight = aParams.minHeight || BUTTON_HEIGHT+2*TAB_PADDING;
	if(this.vertical){
        this.minWidth = aParams.minWidth || BUTTON_WIDTH+2*TAB_PADDING;
        this.minHeight = aParams.minHeight || 2*BUTTON_HEIGHT+2*TAB_PADDING;
    }
	this.startTab = 0;
	if(aParams.elements){
		$.each(jQuery.makeArray(aParams.elements), function(i, e){
			instance.addTab(e);
		});
	}
	if(this.tabs.length>0 && !this.toolbarStyle){
		this.selectTab(this.tabs[0]);
	}
};

core.Tabs.prototype.makeResize = function(aX, aY, aWidth, aHeight){
	this.hideDropDown();
	if(this.tabs.length==0)
		return;
	var tabCount = this.tabs.length;
    this.tabCount = tabCount;
//    core.log('tabs resize', this.instanceID, this.dirty, aWidth, aHeight);
    if(this.dirty){
//        core.log('tabs resize', this.instanceID, tabCount);
        var effectiveWidth = aWidth-(tabCount+1)*TAB_PADDING;
        var tabWidth = Math.floor(effectiveWidth/tabCount);
        if(this.vertical){
            effectiveWidth = aHeight-(tabCount+1)*TAB_PADDING;
        }
        var tabsShow = tabCount;
        var showButtons = false;

        if(this.toolbarStyle){
            var minWidthHeight = 'minWidth';
            var aWidthHeight = aWidth;
            if(this.vertical){
                minWidthHeight = 'minHeight';
                aWidthHeight = aHeight;
            }
            var reqWidth = TAB_PADDING;
            for(var i = 0; i<tabCount; i++){
                reqWidth += this.tabs[i][minWidthHeight]+TAB_PADDING;
            }
    //        core.log(this.instanceID, 'req:', reqWidth, 'awh:', aWidthHeight);
            if(reqWidth>aWidthHeight){
                showButtons = true;
                effectiveWidth = aWidthHeight - 2*BUTTON_WIDTH_ICON-TAB_PADDING;
                var width = TAB_PADDING;
                var enoughWidth = false;
                tabsShow = 0;
                for(var i = this.startTab; i<tabCount; i++){
                    if(width+this.tabs[i][minWidthHeight]+TAB_PADDING>effectiveWidth){
                        enoughWidth = true;
                        break;
                    }else{
                        width += this.tabs[i][minWidthHeight]+TAB_PADDING;
                        tabsShow++;
                    }

                }
                if(!enoughWidth){
                    for(var i = this.startTab-1; i>=0; i--){
                        if(width+this.tabs[i][minWidthHeight]+TAB_PADDING>effectiveWidth){
                            break;
                        }else{
                            width += this.tabs[i][minWidthHeight]+TAB_PADDING;
                            tabsShow++;
                            this.startTab--;
                        }
                    }
                }
            }else{
                showButtons = false;
                this.startTab = 0;
            }
        }else{
            if(tabWidth>=TAB_WIDTH){
                this.startTab = 0;
            }else{
                showButtons = true;
                effectiveWidth -= 2*BUTTON_WIDTH_ICON-TAB_PADDING;
                tabsShow = Math.floor(effectiveWidth/TAB_WIDTH);
                tabWidth = Math.floor(effectiveWidth/tabsShow);
                if(this.startTab+tabsShow>=tabCount){
                    this.startTab = tabCount-tabsShow;
                }
            }
        }
//    	core.log('resize tabs', this.instanceID, tabCount, this.startTab, tabsShow, showButtons, aWidth-BUTTON_WIDTH_ICON);
        if(showButtons){
            this.leftButton.show();
            this.rightButton.show();
            this.leftButton.resize(TAB_PADDING, TAB_PADDING, BUTTON_WIDTH_ICON, BUTTON_HEIGHT);
            if(this.vertical){
                this.rightButton.resize(TAB_PADDING, aHeight-BUTTON_HEIGHT, BUTTON_WIDTH_ICON, BUTTON_HEIGHT);
            }else{
                this.rightButton.resize(aWidth-BUTTON_WIDTH_ICON, TAB_PADDING, BUTTON_WIDTH_ICON, BUTTON_HEIGHT);
            }
        }else{
            this.leftButton.hide();
            this.rightButton.hide();
        }
        this.showButtons = showButtons;
        this.tabWidth = tabWidth;
        this.tabsShow = tabsShow;
    }
//this is after dirty
	if(this.showButtons){
		this.leftButton.setDisabled(this.startTab==0);
		this.rightButton.setDisabled(this.startTab+this.tabsShow>=tabCount);
	}

//this is end


	var leftPadding = this.showButtons?BUTTON_WIDTH_ICON+TAB_PADDING:0;
    if(this.vertical){
        leftPadding = this.showButtons?BUTTON_HEIGHT+TAB_PADDING:0;
    }
	for(var i = 0; i<tabCount; i++){
		var tab = this.tabs[i];
		if(i<this.startTab || i>=this.startTab+this.tabsShow){
			tab.hide();
			continue;
		}
		leftPadding += TAB_PADDING;
		tab.show();
		
//		if(this.startTab+tabsShow-1==i)
//			tabWidth = aWidth-leftPadding-(showButtons?BUTTON_WIDTH_ICON:0)-TAB_PADDING;
		tab.x = leftPadding;
		if(this.toolbarStyle){
            if(this.vertical){
                tab.resize(TAB_PADDING, leftPadding, tab.minWidth, tab.minHeight);
                leftPadding += tab.minHeight;
            }else{
                tab.resize(leftPadding, TAB_PADDING, tab.minWidth, tab.minHeight);
                leftPadding += tab.minWidth;
            }
		}else{
			tab.resize(leftPadding, TAB_PADDING, this.tabWidth, tab.minHeight);
			leftPadding += this.tabWidth;
		}
	}
	
};

core.Tabs.prototype.getDropTarget = function(aDraggable, aX, aY){
	for(var i = 0; i<this.tabs.length; i++){
		var tab = this.tabs[i];
		if(i<this.startTab || i>=this.startTab+this.tabsShow){
			continue;
		}
		var itm = tab.bounds;
		if(itm && itm.x<=aX && itm.y<=aY && itm.x+itm.width>=aX && itm.y+itm.height>=aY && tab.getDropTarget){
			return tab.getDropTarget(aDraggable, aX-itm.x, aY-itm.y);
		}
	}
	return null;
}

//core.Tabs.prototype.refresh = function(){
//	return this.resize(this.x, this.y, this.width, this.height);
//}
//
core.Tabs.prototype.selectTab = function(aTab, aMouseClick){
	this.hideDropDown();
	if(aTab!=this.selectedTab){
		if(this.selectedTab){
			this.selectedTab.unselect();
		}
		this.selectedTab = aTab;		
		this.fire('select', {
            mouse: aMouseClick
        }, aTab);
		this.selectedTab.select();		
	}
}

core.Tabs.prototype.removeTab = function(aTab){
	this.hideDropDown();
	
	for(var i = 0; i<this.tabs.length; i++){
		var t = this.tabs[i];
		if(t==aTab){
			this.tabs.splice(i, 1);
			aTab.hide();
			if(this.selectedTab==aTab && this.tabs.length>0){
				this.selectTab(this.tabs[i<this.tabs.length?i:i-1]);
			}
            this.dirty = true;
			return true;
		}
	}
	return false;
}

core.Tabs.prototype.addTab = function(){
    for (i = 0; i < arguments.length; i++) {
        var aTab = arguments[i];
        this.insertTab(aTab, this.tabs.length);
    }
	return this.tabs.length;
};

core.Tabs.prototype.insertTab = function(aTab, aPosition){
    this.tabs.splice(aPosition, 0, aTab);
    this.reParent(aTab);
    var instance = this;
    aTab.on('select', function(i, e){
        if(i.instanceOf('core.Tab')){
            instance.selectTab(i, true);
            return false;
        }
        return true;
    });
    this.dirty = true;
	return this.tabs.length;
};

core.Menu = function(aParams){
	this.init(aParams);
};

core.extendClass('core.Menu', 'core.Box');

core.Menu.prototype.setElementDisabledState = function(aState, aElement){
	if(aState)
		this.disableElement(aElement);
	else
		this.enableElement(aElement);
}

core.Menu.prototype.disableElement = function(aMenuItem){
	var m = aMenuItem;
	if(!m.disabledDiv){
		m.disabled = true;
		$(m.div).removeClass('menu_item_over');
		m.disabledDiv = core.newDiv(m.control.div, 'disabled');
		$(m.disabledDiv).width(m.control.bounds.width);
		$(m.disabledDiv).height(m.control.bounds.height);
	}
}

core.Menu.prototype.enableElement = function(aMenuItem){
	var m = aMenuItem;
	if(m.disabledDiv){
		m.disabled = false;
		$(m.disabledDiv).remove();
		m.disabledDiv = null;
	}
}

core.Menu.prototype.hideMenu = function(){
    $(this.div).remove();
}

core.Menu.prototype.init = function(config){
	this.superCall('init', arguments, config);
	config.parent.reParent(this);
	var instance = this;
	var tm = config.menu || [];
    this.menu = [];
    for (var i = 0; i < tm.length; i++) {
        this.menu[i] = tm[i];
    }

    this.owner = config.owner;
	this.topDown = config.topDown;
	this.startPointX = config.startPointX;
	this.startPointY = config.startPointY;
	this.areaWidth = config.areaWidth || MENU_MIN_WIDTH;
	this.areaHeight = config.areaHeight || MENU_ITEM_HEIGHT;
	if(this.topDown)
		this.areaHeight = this.areaHeight-this.startPointY;
	else
		this.areaHeight = this.startPointY;	
	var calcMenuHeight = function(aItems){
		return aItems*(MENU_ITEM_HEIGHT+MENU_PADDING)+MENU_PADDING;
	}
	$(this.div).addClass('menu');
	this.showFrom = 1;
	this.showTo = this.menu.length-2;
	if(calcMenuHeight(this.menu.length)>this.areaHeight){
		
		this.showTo = Math.floor((this.areaHeight-MENU_PADDING)/(MENU_ITEM_HEIGHT+MENU_PADDING))-2;
		this.menu.splice(0, 0, {
			cls: 'tab_icon tab_up',
			text: 'Scroll up',
			align: 'center',
			disabled: true,
			keepOnClick: true,
			handler: function(){
				if(instance.showFrom>1){
					instance.showFrom--;
					instance.showTo--;
				}
				instance.setElementDisabledState(instance.showFrom==1, instance.menu[0]);
				instance.setElementDisabledState(instance.showTo==instance.menuCount-2, instance.menu[instance.menuCount-1]);
				instance.showFrame();
				return false;
			}
		});
		this.menu.push({
			cls: 'tab_icon tab_down',
			text: 'Scroll down',
			align: 'center',
			keepOnClick: true,
			handler: function(){
				if(instance.showTo<instance.menuCount-2){
					instance.showFrom++;
					instance.showTo++;
				}
				instance.setElementDisabledState(instance.showFrom==1, instance.menu[0]);
				instance.setElementDisabledState(instance.showTo==instance.menuCount-2, instance.menu[instance.menuCount-1]);
				instance.showFrame();				
				return false;
			}
		});
	}
	this.menuCount = this.menu.length;
	var menuWidth = 0;
	for(var i = 0; i<this.menuCount; i++){//Create menu items
		var m = this.menu[i];
		m.control = new core.OneLineHelper({
			padding: 3,
			text: m.getText? m.getText(): m.text,
			icons: m.icons,
			parent: this,
			icon: m.icon,
            icon_cls: m.cls,
			cls: 'mouse_over',
			align: m.align
		});
		m.control.menuItem = m;
		m.control.on('mouseover', function(i){
			if(!i.menuItem.disabled)
				$(i.div).addClass('menu_item_over');
		});
		
		m.control.on('mouseout', function(i){
			$(i.div).removeClass('menu_item_over');
		});
		
		m.control.on('click', function(i, e){
			if(!i.menuItem.disabled){
				var result = true;
				if(i.menuItem.handler){
					result = i.menuItem.handler(i.menuItem, instance.owner, e);
				}
				if(result)
					result = instance.fire('select', e, i.menuItem);
                if(result)
                    instance.hideMenu();
			}
			return false;
		});
		
		var itemWidth = m.control.getTextWidth()+6;
		if(itemWidth>menuWidth)
			menuWidth = itemWidth;
		m.control.hide();
	}
	menuWidth += MENU_PADDING*2;
	if(menuWidth<MENU_MIN_WIDTH)
		menuWidth = MENU_MIN_WIDTH;
	if(menuWidth>this.areaWidth)
		menuWidth = this.areaWidth;
	if(this.startPointX+menuWidth>this.areaWidth)
		this.startPointX = this.areaWidth-menuWidth;
	var menuHeight = calcMenuHeight(this.showTo-this.showFrom+3);
	$(this.div).width(menuWidth);
	$(this.div).height(menuHeight);
	$(this.div).css('left', this.startPointX);
	$(this.div).css('top', this.startPointY-(!this.topDown?menuHeight:0));
	this.menuWidth = menuWidth;
	this.showFrame();
};

core.Menu.prototype.showFrame = function(){
	this.hide();
	for(var i = 0; i<this.menuCount; i++){
		var m = this.menu[i];		
		m.control.hide();
		if(m.disabled){
			this.enableElement(m);
			m.disabled = true;
		}
	}
	var x = MENU_PADDING;
	var y = MENU_PADDING;
	this.show();
	for(var i = 0; i<this.menuCount; i++){//Create menu items
		if(i==0 || i == this.menuCount-1 || (i>=this.showFrom && i<=this.showTo)){
			var m = this.menu[i];
            m.control.show();
			m.control.resize(x, y, this.menuWidth - MENU_PADDING*2, MENU_ITEM_HEIGHT);
			if(m.disabled)
				this.disableElement(m);
			y += MENU_ITEM_HEIGHT+MENU_PADDING;			
		}
	}	
//	this.setVisibility(true);
};

core.Tabs.prototype.hideDropDown = function(){
	if(this.dropDown){
		$(this.dropDown.div).remove();
		this.dropDown = null;
        return true;
	}
    return false;
};
core.showMenu = function(aElement, aMenuArray, aTopDown, aVertical){
    var element = aElement;
    var pos = element.getPosition();
	var dropDown = new core.Menu({
		menu: aMenuArray,
        owner: aElement,
		areaWidth: core.desktop.width,
		areaHeight: core.desktop.height,
		startPointX: TAB_PADDING+aVertical?(pos.x):(pos.x),
		startPointY: pos.y+(aTopDown?element.minHeight:0)+TAB_PADDING,
		parent: core.desktop,
		topDown: aTopDown
	});
    return dropDown;
}
core.Tabs.prototype.showDropDown = function(aTab, aMenuArray, aTopDown, aVertical, aElement){
    var element = aElement || this;
//	core.log('Drop down menu here, ', element.parent.width, element.parent.height, element.x, element.y);
    this.menuTab = aTab;
	this.dropDown = new core.Menu({
		menu: aMenuArray,
        owner: aTab,
		areaWidth: element.parent.width-TAB_PADDING,
		areaHeight: element.parent.height,
		startPointX: aVertical?(element.x-TAB_PADDING):(aTab.x+element.x),
		startPointY: element.y+(aTopDown?element.minHeight:0),
		parent: element.parent,
		topDown: aTopDown
	});
	var instance = this;
	this.dropDown.on('select', function(i, e, item){
		instance.hideDropDown();
        if(instance.menuTab)
            instance.menuTab.fire('menu_select', e, instance.selectedTab, item);
	});
};
